package hikari.preferences.device_parameters;

import com.gooddies.wiring.Wiring;
import hikari.persistence.propertieseditor.EditorPanel;
import hikari.persistence.propertieseditor.editors.HelpProvider;
import hikari.gui.AskDialog;
import hikari.utils.Utils;
import java.awt.Window;
import java.io.InputStream;
import javax.swing.JPanel;

/**
 * @author sad
 */
public class DeviceConfigurationDialog extends AskDialog {

    private EditorPanel editor;
    private DeviceParameters params;

    public DeviceConfigurationDialog(Window parent, DeviceParameters params) {
        super(parent);
        setTitle("Device settings");
        this.params = params;
    }

    @Override
    public String getDialogName() {
        return "DeviceConfiguration";
    }

    private HelpProvider getHelpProvider() {
        String docPath = "/hikari/resource/docs/deviceConfiguration.xml";
        InputStream stream = DeviceConfigurationDialog.class.getResourceAsStream(docPath);
        if (stream == null) {
            throw new RuntimeException("Help file cannot be found in resources " + docPath);
        }
        XMLHelpManager helpProvider = null;
        try {
            helpProvider = new XMLHelpManager(stream);
        } catch (Exception ex) {
            Wiring.getComponent(Utils.class).logError(ex);
        }
        return helpProvider;
    }

    @Override
    protected void createContent(JPanel panel) {
        editor = new EditorPanel();
        editor.setHelpProvider(getHelpProvider());
        editor.fillPanelWithData(params);
        panel.add(editor);
    }

    @Override
    protected boolean onOK() {
        editor.saveBackToObject();
        return true;
    }
}
